<?php

/**
 * ecshop ERP v1.0
 * ----------------------------------------------------------------------------
 * http://www.phpally.com
 * Jacklee的博客 致力于php技术
 * ----------------------------------------------------------------------------
 * 作者: Jacklee
 * 邮箱: jack349392900@gmail.com
 * 创建时间: 2013-11-18
 * 最后修改时间: 2013-11-18
 */

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
require(dirname(__FILE__) . '/includes/lib/lib_erp_base.php');
require(dirname(__FILE__) . '/includes/lib/lib_erp_common.php');
require(dirname(__FILE__) . '/includes/lib/lib_erp_supplier.php');


if($_REQUEST['act'] == 'list')
{
	if((admin_priv('erp_sys_manage', '', false)))
	{	
		include('./includes/lib/page.class.php');
		
		//分页类参数
		$num_per_page=10;
		$mode=1;
		$page_bar_num=6;
		$page_style="page_style";//页码css类
		$current_page_style="current_page_style";//当前页css类					

		$page=isset($_REQUEST['page'])?($_REQUEST['page']):1;
		$start=$num_per_page*($page-1);	
		
		$total_num=count(get_supplier_group());

		$supplier_groups=get_supplier_group('',$start,$num_per_page);
		$url="erp_supplier_group.php?act=list";
		
		$smarty->assign('url',  $_SERVER["REQUEST_URI"].'&page='.$page);
		
		$page=new page(array('total_data'=>$total_num,'data_per_page'=>$num_per_page,'url'=>$url,'mode'=>$mode,'page_bar_num'=>$page_bar_num,'page_style'=>$page_style,'current_page_style'=>$current_page_style));
		$smarty->assign('page',  $page->show());			
	
		$action_link = array('href' => 'erp_supplier_group.php?act=add_supplier_group', 'text' => $_LANG['erp_add_supplier_group']);
    $smarty->assign('action_link',  $action_link);
    	
		$smarty->assign('supplier_groups',  $supplier_groups);
		
		$smarty->assign('admins',  get_admin());
		
		$smarty->assign('ur_here', $GLOBALS['_LANG']['erp_supplier_group']);
		
		$smarty->display('erp_supplier_group_list.htm');
	}
	else{
		$href="./admin/index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}		
}

elseif($_REQUEST['act'] == 'change_is_valid')
{
	include('../includes/cls_json.php');
	$json  = new JSON;
	
	if(!admin_priv('erp_sys_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
	else{
		$group_id=$_REQUEST['group_id'];
		$group_info=get_supplier_group($group_id);
		$is_valid=$group_info['is_valid'];
		if($is_valid==1)
		{
			$sql="update ".$ecs->table('erp_supplier_group')." set is_valid='0' where group_id='".$group_id."'";
			$db->query($sql);
			
			$result['error']=0;
			$result['is_valid']=0;
			die($json->encode($result));
		}
		elseif($is_valid==0)
		{
			$sql="update ".$ecs->table('erp_supplier_group')." set is_valid='1' where group_id='".$group_id."'";
			$db->query($sql);
			
			$result['error']=0;
			$result['is_valid']=1;
			die($json->encode($result));			
		}		
	}
}

elseif($_REQUEST['act'] == 'change_group_admin')
{
	include('../includes/cls_json.php');
	$json  = new JSON;
	
	if(!admin_priv('erp_sys_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
	else{
		$group_id=isset($_REQUEST['group_id'])? intval($_REQUEST['group_id']) : 0;
		$admin_id=isset($_REQUEST['admin_id'])? intval($_REQUEST['admin_id']) : 0;
		
		if(empty($group_id) || empty($admin_id))
		{
			$result['error']=2;
			$result['message']=$_LANG['erp_wrong_parameter'];
			die($json->encode($result));				
		}
		
		$sql="update ".$ecs->table('erp_supplier_group')." set admin_id='".$admin_id."' where group_id='".$group_id."' limit 1";
		$db->query($sql);
		
		$result['error']=0;
		die($json->encode($result));			
	}	
}


elseif($_REQUEST['act'] == 'edit_supplier_group')
{
	if((admin_priv('erp_sys_manage', '', false)))
	{
		$group_id=$_REQUEST['group_id'];

		$smarty->assign('admins',  get_admin());
		
		$smarty->assign('supplier_group',  get_supplier_group($group_id));
		
		$smarty->assign('ur_here', $GLOBALS['_LANG']['erp_edit_supplier_group']);
		
		$smarty->display('erp_supplier_group.htm');
	}
	else{
		$href="./admin/index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   		sys_msg($_LANG['erp_no_permit'], 0, $link);			
	}
}

elseif($_REQUEST['act'] == 'delete_supplier_group')
{
	include('../includes/cls_json.php');
	$json  = new JSON;
	
	if(!admin_priv('erp_sys_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
	else{
		$group_id=isset($_REQUEST['group_id'])? intval($_REQUEST['group_id']) : 0;
		if(empty($group_id))
		{
			$result['error']=2;
			$result['message']=$_LANG['erp_wrong_parameter'];
			die($json->encode($result));				
		}		
		
		if(delete_supplier_group($group_id))
		{
			$result['error']=0;
			die($json->encode($result));					
		}
		else{
			$result['error']=3;
			$result['message']=$_LANG['erp_supplier_group_has_supplier'];
			die($json->encode($result));					
		}
	}
}

elseif($_REQUEST['act'] == 'add_supplier_group')
{
	if((admin_priv('erp_sys_manage', '', false)))
	{
		$smarty->assign('admins',  get_admin());
		
		$smarty->assign('ur_here', $GLOBALS['_LANG']['erp_add_supplier_group']);
		
		$smarty->display('erp_supplier_group.htm');	
	}
	else{
		$href="./admin/index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}
}

elseif($_REQUEST['act'] == 'save_supplier_group')
{
	include('../includes/cls_json.php');
	$json  = new JSON;	

	if(!admin_priv('erp_sys_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
	else{
		$group_id=intval($_REQUEST['group_id']);
		$is_valid=intval($_REQUEST['is_valid']);
		$group_name=trim($_REQUEST['group_name']);
		$group_admin=intval($_REQUEST['group_admin']);
		
		if(empty($group_id))
		{
			$sql="insert into ".$ecs->table('erp_supplier_group')." set group_name='".$group_name."',admin_id='".$group_admin."',is_valid='".$is_valid."'";
			$db->query($sql);			
		}
		else{
			$sql="update ".$ecs->table('erp_supplier_group')." set group_name='".$group_name."',admin_id='".$group_admin."',is_valid='".$is_valid."' where group_id='".$group_id."'";
			$db->query($sql);	
		}
		$result['error']=0;
		die($json->encode($result));		
	}
}


?>